package com.admin.web.controller;

import com.admin.web.uresponse.OBGUserResponse;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.activity.ObgRechargeActivity;
import com.bayss.core.entity.rbac.ObgOperateLog;
import com.bayss.core.service.OperateLogService;
import com.bayss.core.service.TransactionRecordService;
import com.bayss.core.service.activity.ObgRechargeActivityService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 储值活动管理
 * Created by LDL on 2020/1/9.
 */
@RestController
public class RechargeActivityController extends ApiBaseController {
    @Autowired
    private ObgRechargeActivityService obgRechargeActivityService;
    @Autowired
    private TransactionRecordService transactionRecordService;
    @Autowired
    private OperateLogService operateLogService;
    /**
     * 删除
     */
    @GetMapping(value = "/rechargeActivity/delete.json")
    public ResultMsg deleteById(Integer id) {
        try {
            obgRechargeActivityService.deleteObgRechargeActivity(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "失败");
        }
        return ResultMsg.success();
    }

    /**
     * 新增
     */
    @PostMapping(value = "/rechargeActivity/add.json")
    public ResultMsg add(OBGUserResponse userResponse, @RequestBody ObgRechargeActivity rechargeActivity) {
        rechargeActivity.setOperatorId(userResponse.getUid());
        obgRechargeActivityService.saveObgRechargeActivity(rechargeActivity);
        return ResultMsg.success();
    }

    /**
     * 修改
     */
    @PostMapping(value = "/rechargeActivity/update.json")
    public ResultMsg update(@RequestBody ObgRechargeActivity rechargeActivity) {
        obgRechargeActivityService.updateObgRechargeActivity(rechargeActivity);
        return ResultMsg.success();
    }

    /**
     * 上架
     */
    @GetMapping(value = "/rechargeActivity/usable.json")
    public ResultMsg usable(Integer id) {
        ObgRechargeActivity activity = new ObgRechargeActivity();
        activity.setId(id);
        activity.setStatus(Byte.valueOf("1"));
        obgRechargeActivityService.updateObgRechargeActivity(activity);
        return ResultMsg.success();
    }
    /**
     * 下架
     */
    @GetMapping(value = "/rechargeActivity/disable.json")
    public ResultMsg disable(Integer id) {
        ObgRechargeActivity activity = new ObgRechargeActivity();
        activity.setId(id);
        activity.setStatus(Byte.valueOf("0"));
        obgRechargeActivityService.updateObgRechargeActivity(activity);
        return ResultMsg.success();
    }


    /**
     * 分页查询
     */
    @GetMapping(value = "/rechargeActivity/listPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize) {
        try {
            PageInfo<ObgRechargeActivity> pageInfo = obgRechargeActivityService.listPages(page, pageSize);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 查询所有可用
     *
     * @return
     */
    @GetMapping(value = "/rechargeActivity/list.json")
    public ResultMsg getByPage() {
        try {
            return ResultMsg.success(obgRechargeActivityService.list());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * 交易记录查询
     *
     * @param transactionType 交易类型
     * @param startTime       yyyy-MM-dd HH:mm:ss
     */
    @RequestMapping("/transaction/recordList.json")
    public ResultMsg transactionRecordList(Long uid, Integer transactionType, String startTime, String endTime, String cellphone, Integer page, Integer pageSize) {
        return ResultMsg.success(transactionRecordService.list(uid, transactionType, cellphone,startTime, endTime,page,pageSize));
    }


    /**
     * 操作记录查询
     */
    @GetMapping(value = "/operateLog/listPage.json")
    public ResultMsg operateLogPages(Integer page, Integer pageSize) {
        PageInfo<ObgOperateLog> pageInfo = operateLogService.listPages(null,page, pageSize);
        return ResultMsg.success(pageInfo);
    }
}
